<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="robots" content="noodp" />
        <title>ArrayList源码阅读（中）——迭代器和子集合 - MC&#39; blog</title><meta name="Description" content="MC&#39; Blog，不定期的技术分享"><meta property="og:title" content="ArrayList源码阅读（中）——迭代器和子集合" />
<meta property="og:description" content="ArrayList源码阅读（中） 中篇的主要内容是ArrayList的迭代器和子集合的问题。 首先，我们追溯到集合的源头Collectio这个接" />
<meta property="og:type" content="article" />
<meta property="og:url" content="http://example.org/arraylist-%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E4%B8%AD/" /><meta property="og:image" content="http://example.org/logo.png"/><meta property="article:section" content="posts" />
<meta property="article:published_time" content="2018-05-29T17:22:00+08:00" />
<meta property="article:modified_time" content="2018-05-29T17:22:00+08:00" /><meta property="og:site_name" content="MC&#39; Blog" />
<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:image" content="http://example.org/logo.png"/>

<meta name="twitter:title" content="ArrayList源码阅读（中）——迭代器和子集合"/>
<meta name="twitter:description" content="ArrayList源码阅读（中） 中篇的主要内容是ArrayList的迭代器和子集合的问题。 首先，我们追溯到集合的源头Collectio这个接"/>
<meta name="application-name" content="MC&#39; Blog">
<meta name="apple-mobile-web-app-title" content="MC&#39; Blog"><meta name="theme-color" content="#ffffff"><meta name="msapplication-TileColor" content="#da532c"><link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
        <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
        <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png"><link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png"><link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5"><link rel="manifest" href="/site.webmanifest"><link rel="canonical" href="http://example.org/arraylist-%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E4%B8%AD/" /><link rel="prev" href="http://example.org/arraylist%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E4%B8%8A/" /><link rel="next" href="http://example.org/arraylist%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E4%B8%8B/" /><link rel="stylesheet" href="/css/style.min.css"><link rel="preload" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.1.1/css/all.min.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
        <noscript><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.1.1/css/all.min.css"></noscript><link rel="preload" href="https://cdn.jsdelivr.net/npm/animate.css@4.1.1/animate.min.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
        <noscript><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@4.1.1/animate.min.css"></noscript><script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "BlogPosting",
        "headline": "ArrayList源码阅读（中）——迭代器和子集合",
        "inLanguage": "zh-CN",
        "mainEntityOfPage": {
            "@type": "WebPage",
            "@id": "http:\/\/example.org\/arraylist-%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E4%B8%AD\/"
        },"image": ["http:\/\/example.org\/images\/Apple-Devices-Preview.png"],"genre": "posts","keywords": "Java","wordcount":  5033 ,
        "url": "http:\/\/example.org\/arraylist-%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E4%B8%AD\/","datePublished": "2018-05-29T17:22:00+08:00","dateModified": "2018-05-29T17:22:00+08:00","license": "This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.","publisher": {
            "@type": "Organization",
            "name": "xxxx","logo": {
                    "@type": "ImageObject",
                    "url": "http:\/\/example.org\/images\/avatar.png",
                    "width":  528 ,
                    "height":  560 
                }},"author": {
                "@type": "Person",
                "name": "MC"
            },"description": ""
    }
    </script></head>
    <body data-header-desktop="fixed" data-header-mobile="auto"><script type="text/javascript">(window.localStorage && localStorage.getItem('theme') ? localStorage.getItem('theme') === 'dark' : ('auto' === 'auto' ? window.matchMedia('(prefers-color-scheme: dark)').matches : 'auto' === 'dark')) && document.body.setAttribute('theme', 'dark');</script>

        <div id="mask"></div><div class="wrapper"><header class="desktop" id="header-desktop">
    <div class="header-wrapper">
        <div class="header-title">
            <a href="/" title="MC&#39; blog"><span class="header-title-pre"><i class='far fa-kiss-wink-heart fa-fw' aria-hidden='true'></i></span>MC&#39; Blog</a>
        </div>
        <div class="menu">
            <div class="menu-inner"><a class="menu-item" href="/posts/"> 所有文章 </a><a class="menu-item" href="/tags/"> 标签 </a><a class="menu-item" href="/categories/"> 分类 </a><a class="menu-item" href="/categories/documentation/"> 文档 </a><a class="menu-item" href="/about/"> 关于 </a><a class="menu-item" href="https://github.com/woxin123" title="GitHub" rel="noopener noreffer" target="_blank"><i class='fab fa-github fa-fw' aria-hidden='true'></i>  </a><span class="menu-item delimiter"></span><span class="menu-item search" id="search-desktop">
                        <input type="text" placeholder="搜索文章标题或内容..." id="search-input-desktop">
                        <a href="javascript:void(0);" class="search-button search-toggle" id="search-toggle-desktop" title="搜索">
                            <i class="fas fa-search fa-fw" aria-hidden="true"></i>
                        </a>
                        <a href="javascript:void(0);" class="search-button search-clear" id="search-clear-desktop" title="清空">
                            <i class="fas fa-times-circle fa-fw" aria-hidden="true"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-desktop">
                            <i class="fas fa-spinner fa-fw fa-spin" aria-hidden="true"></i>
                        </span>
                    </span><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                    <i class="fas fa-adjust fa-fw" aria-hidden="true"></i>
                </a></div>
        </div>
    </div>
</header><header class="mobile" id="header-mobile">
    <div class="header-container">
        <div class="header-wrapper">
            <div class="header-title">
                <a href="/" title="MC&#39; blog"><span class="header-title-pre"><i class='far fa-kiss-wink-heart fa-fw' aria-hidden='true'></i></span>MC&#39; Blog</a>
            </div>
            <div class="menu-toggle" id="menu-toggle-mobile">
                <span></span><span></span><span></span>
            </div>
        </div>
        <div class="menu" id="menu-mobile"><div class="search-wrapper">
                    <div class="search mobile" id="search-mobile">
                        <input type="text" placeholder="搜索文章标题或内容..." id="search-input-mobile">
                        <a href="javascript:void(0);" class="search-button search-toggle" id="search-toggle-mobile" title="搜索">
                            <i class="fas fa-search fa-fw" aria-hidden="true"></i>
                        </a>
                        <a href="javascript:void(0);" class="search-button search-clear" id="search-clear-mobile" title="清空">
                            <i class="fas fa-times-circle fa-fw" aria-hidden="true"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-mobile">
                            <i class="fas fa-spinner fa-fw fa-spin" aria-hidden="true"></i>
                        </span>
                    </div>
                    <a href="javascript:void(0);" class="search-cancel" id="search-cancel-mobile">
                        取消
                    </a>
                </div><a class="menu-item" href="/posts/" title="">所有文章</a><a class="menu-item" href="/tags/" title="">标签</a><a class="menu-item" href="/categories/" title="">分类</a><a class="menu-item" href="/categories/documentation/" title="">文档</a><a class="menu-item" href="/about/" title="">关于</a><a class="menu-item" href="https://github.com/woxin123" title="GitHub" rel="noopener noreffer" target="_blank"><i class='fab fa-github fa-fw' aria-hidden='true'></i></a><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                <i class="fas fa-adjust fa-fw" aria-hidden="true"></i>
            </a></div>
    </div>
</header><div class="search-dropdown desktop">
        <div id="search-dropdown-desktop"></div>
    </div>
    <div class="search-dropdown mobile">
        <div id="search-dropdown-mobile"></div>
    </div><main class="main">
                <div class="container"><div class="toc" id="toc-auto">
            <h2 class="toc-title">目录</h2>
            <div class="toc-content" id="toc-content-auto"></div>
        </div><article class="page single"><h1 class="single-title animate__animated animate__flipInX">ArrayList源码阅读（中）——迭代器和子集合</h1><div class="post-meta">
            <div class="post-meta-line"><span class="post-author"><a href="/" title="Author" rel="author" class="author"><i class="fas fa-user-circle fa-fw" aria-hidden="true"></i>MC</a></span>&nbsp;<span class="post-category">收录于 <a href="/categories/java/"><i class="far fa-folder fa-fw" aria-hidden="true"></i>Java</a></span></div>
            <div class="post-meta-line"><i class="far fa-calendar-alt fa-fw" aria-hidden="true"></i>&nbsp;<time datetime="2018-05-29">2018-05-29</time>&nbsp;<i class="fas fa-pencil-alt fa-fw" aria-hidden="true"></i>&nbsp;约 5033 字&nbsp;
                <i class="far fa-clock fa-fw" aria-hidden="true"></i>&nbsp;预计阅读 11 分钟&nbsp;<span id="/arraylist-%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E4%B8%AD/" class="leancloud_visitors" data-flag-title="ArrayList源码阅读（中）——迭代器和子集合">
                        <i class="far fa-eye fa-fw" aria-hidden="true"></i>&nbsp;<span class=leancloud-visitors-count></span>&nbsp;次阅读
                    </span>&nbsp;</div>
        </div><div class="details toc" id="toc-static"  data-kept="">
                <div class="details-summary toc-title">
                    <span>目录</span>
                    <span><i class="details-icon fas fa-angle-right" aria-hidden="true"></i></span>
                </div>
                <div class="details-content toc-content" id="toc-content-static"><nav id="TableOfContents">
  <ul>
    <li><a href="#arraylist源码阅读中">ArrayList源码阅读（中）</a>
      <ul>
        <li>
          <ul>
            <li><a href="#arraylist中的子集合">ArrayList中的子集合</a></li>
          </ul>
        </li>
      </ul>
    </li>
  </ul>
</nav></div>
            </div><div class="content" id="content"><h2 id="arraylist源码阅读中">ArrayList源码阅读（中）</h2>
<p>中篇的主要内容是ArrayList的迭代器和子集合的问题。
首先，我们追溯到集合的源头Collectio这个接口：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kd">public</span> <span class="kd">interface</span> <span class="nc">Collection</span><span class="o">&lt;</span><span class="n">E</span><span class="o">&gt;</span> <span class="kd">extends</span> <span class="n">Iterable</span><span class="o">&lt;</span><span class="n">E</span><span class="o">&gt;</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">	<span class="kt">int</span> <span class="nf">size</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">	<span class="kt">boolean</span> <span class="nf">isEmpty</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">	<span class="kt">boolean</span> <span class="nf">contains</span><span class="o">(</span><span class="n">Object</span> <span class="n">o</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">	<span class="n">Iterator</span><span class="o">&lt;</span><span class="n">E</span><span class="o">&gt;</span> <span class="nf">iterator</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">	<span class="n">Object</span><span class="o">[]</span> <span class="nf">toArray</span><span class="o">();</span>
</span></span><span class="line"><span class="cl"> 	<span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span> <span class="n">T</span><span class="o">[]</span> <span class="nf">toArray</span><span class="o">(</span><span class="n">T</span><span class="o">[]</span> <span class="n">a</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">	<span class="kt">boolean</span> <span class="nf">add</span><span class="o">(</span><span class="n">E</span> <span class="n">e</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">	<span class="kt">boolean</span> <span class="nf">remove</span><span class="o">(</span><span class="n">Object</span> <span class="n">o</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">	<span class="kt">boolean</span> <span class="nf">containsAll</span><span class="o">(</span><span class="n">Collection</span><span class="o">&lt;?&gt;</span> <span class="n">c</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">	<span class="kt">boolean</span> <span class="nf">addAll</span><span class="o">(</span><span class="n">Collection</span><span class="o">&lt;?</span> <span class="kd">extends</span> <span class="n">E</span><span class="o">&gt;</span> <span class="n">c</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">	<span class="kt">boolean</span> <span class="nf">removeAll</span><span class="o">(</span><span class="n">Collection</span><span class="o">&lt;?&gt;</span> <span class="n">c</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">	<span class="k">default</span> <span class="kt">boolean</span> <span class="nf">removeIf</span><span class="o">(</span><span class="n">Predicate</span><span class="o">&lt;?</span> <span class="kd">super</span> <span class="n">E</span><span class="o">&gt;</span> <span class="n">filter</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">Objects</span><span class="o">.</span><span class="na">requireNonNull</span><span class="o">(</span><span class="n">filter</span><span class="o">);</span>
</span></span><span class="line"><span class="cl"> 		<span class="kt">boolean</span> <span class="n">removed</span> <span class="o">=</span> <span class="kc">false</span><span class="o">;</span>
</span></span><span class="line"><span class="cl"> 		<span class="kd">final</span> <span class="n">Iterator</span><span class="o">&lt;</span><span class="n">E</span><span class="o">&gt;</span> <span class="n">each</span> <span class="o">=</span> <span class="n">iterator</span><span class="o">();</span>
</span></span><span class="line"><span class="cl"> 		<span class="k">while</span> <span class="o">(</span><span class="n">each</span><span class="o">.</span><span class="na">hasNext</span><span class="o">())</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">        	<span class="k">if</span> <span class="o">(</span><span class="n">filter</span><span class="o">.</span><span class="na">test</span><span class="o">(</span><span class="n">each</span><span class="o">.</span><span class="na">next</span><span class="o">()))</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">            	<span class="n">each</span><span class="o">.</span><span class="na">remove</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">  				<span class="n">removed</span> <span class="o">=</span> <span class="kc">true</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">  			<span class="o">}</span>
</span></span><span class="line"><span class="cl">        <span class="o">}</span>
</span></span><span class="line"><span class="cl">     	<span class="k">return</span> <span class="n">removed</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">  	<span class="o">}</span>
</span></span><span class="line"><span class="cl">	<span class="kt">boolean</span> <span class="nf">retainAll</span><span class="o">(</span><span class="n">Collection</span><span class="o">&lt;?&gt;</span> <span class="n">c</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">	<span class="kt">boolean</span> <span class="nf">equals</span><span class="o">(</span><span class="n">Object</span> <span class="n">o</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">  	<span class="kt">int</span> <span class="nf">hashCode</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">  	<span class="nd">@Override</span>
</span></span><span class="line"><span class="cl">  	<span class="k">default</span> <span class="n">Spliterator</span><span class="o">&lt;</span><span class="n">E</span><span class="o">&gt;</span> <span class="nf">spliterator</span><span class="o">()</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">        <span class="k">return</span> <span class="n">Spliterators</span><span class="o">.</span><span class="na">spliterator</span><span class="o">(</span><span class="k">this</span><span class="o">,</span> <span class="mi">0</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">  	<span class="o">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">  	<span class="k">default</span> <span class="n">Stream</span><span class="o">&lt;</span><span class="n">E</span><span class="o">&gt;</span> <span class="nf">stream</span><span class="o">()</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">    	    <span class="k">return</span> <span class="n">StreamSupport</span><span class="o">.</span><span class="na">stream</span><span class="o">(</span><span class="n">spliterator</span><span class="o">(),</span> <span class="kc">false</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">  	<span class="o">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">	<span class="k">default</span> <span class="n">Stream</span><span class="o">&lt;</span><span class="n">E</span><span class="o">&gt;</span> <span class="nf">parallelStream</span><span class="o">()</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">		<span class="k">return</span> <span class="n">StreamSupport</span><span class="o">.</span><span class="na">stream</span><span class="o">(</span><span class="n">spliterator</span><span class="o">(),</span> <span class="kc">true</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">	<span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>Collection接口继承于Iterable接口，这个接口的作用是用来说明实现这个接口的类是可以迭代的。
Iterable的源码如下：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kd">public</span> <span class="kd">interface</span> <span class="nc">Iterable</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">	<span class="n">Iterator</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span> <span class="nf">iterator</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">    <span class="k">default</span> <span class="kt">void</span> <span class="nf">forEach</span><span class="o">(</span><span class="n">Consumer</span><span class="o">&lt;?</span> <span class="kd">super</span> <span class="n">T</span><span class="o">&gt;</span> <span class="n">action</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">Objects</span><span class="o">.</span><span class="na">requireNonNull</span><span class="o">(</span><span class="n">action</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">        <span class="k">for</span> <span class="o">(</span><span class="n">T</span> <span class="n">t</span> <span class="o">:</span> <span class="k">this</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">            <span class="n">action</span><span class="o">.</span><span class="na">accept</span><span class="o">(</span><span class="n">t</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">        <span class="o">}</span>
</span></span><span class="line"><span class="cl">    <span class="o">}</span>
</span></span><span class="line"><span class="cl">    <span class="k">default</span> <span class="n">Spliterator</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span> <span class="nf">spliterator</span><span class="o">()</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">        <span class="k">return</span> <span class="n">Spliterators</span><span class="o">.</span><span class="na">spliteratorUnknownSize</span><span class="o">(</span><span class="n">iterator</span><span class="o">(),</span> <span class="mi">0</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">    <span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p><strong>该类中有一个iterator，所以实现该接口的类是可迭代的。Iterator才是迭代器。</strong>
在Iterator接口中定义了如下几个方法：</p>
<ul>
<li><code>boolean hasNext()</code> 判断集合中是否有下一个元素。</li>
<li><code>E next()</code> 返回集合中的下一个元素。</li>
<li><code>void remove()</code> 默认的remove方法</li>
<li><code>void forEachRemaining(Consumer&lt;? super E&gt; action)</code> 变量集合中的元素，并根据传入的action进行相关的操作。
下面的这个方法是ArrayList中的跌点器。
下面的是Iterator的源码：</li>
</ul>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kd">public</span> <span class="kd">interface</span> <span class="nc">Iterator</span><span class="o">&lt;</span><span class="n">E</span><span class="o">&gt;</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">    <span class="kt">boolean</span> <span class="nf">hasNext</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">    <span class="n">E</span> <span class="nf">next</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">    <span class="k">default</span> <span class="kt">void</span> <span class="nf">remove</span><span class="o">()</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">        <span class="k">throw</span> <span class="k">new</span> <span class="n">UnsupportedOperationException</span><span class="o">(</span><span class="s">&#34;remove&#34;</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">    <span class="o">}</span>
</span></span><span class="line"><span class="cl">    <span class="k">default</span> <span class="kt">void</span> <span class="nf">forEachRemaining</span><span class="o">(</span><span class="n">Consumer</span><span class="o">&lt;?</span> <span class="kd">super</span> <span class="n">E</span><span class="o">&gt;</span> <span class="n">action</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">Objects</span><span class="o">.</span><span class="na">requireNonNull</span><span class="o">(</span><span class="n">action</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">        <span class="k">while</span> <span class="o">(</span><span class="n">hasNext</span><span class="o">())</span>
</span></span><span class="line"><span class="cl">            <span class="n">action</span><span class="o">.</span><span class="na">accept</span><span class="o">(</span><span class="n">next</span><span class="o">());</span>
</span></span><span class="line"><span class="cl">    <span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>在ArrayList中有一个返回迭代器的方法，如下：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kd">public</span> <span class="n">Iterator</span><span class="o">&lt;</span><span class="n">E</span><span class="o">&gt;</span> <span class="nf">iterator</span><span class="o">()</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">	<span class="k">return</span> <span class="k">new</span> <span class="n">Itr</span><span class="o">();</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>方法中的Itr是ArrayList中的一个内部类，该类继承了Iterator这个接口。
这个接口的源码如下：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span><span class="lnt">44
</span><span class="lnt">45
</span><span class="lnt">46
</span><span class="lnt">47
</span><span class="lnt">48
</span><span class="lnt">49
</span><span class="lnt">50
</span><span class="lnt">51
</span><span class="lnt">52
</span><span class="lnt">53
</span><span class="lnt">54
</span><span class="lnt">55
</span><span class="lnt">56
</span><span class="lnt">57
</span><span class="lnt">58
</span><span class="lnt">59
</span><span class="lnt">60
</span><span class="lnt">61
</span><span class="lnt">62
</span><span class="lnt">63
</span><span class="lnt">64
</span><span class="lnt">65
</span><span class="lnt">66
</span><span class="lnt">67
</span><span class="lnt">68
</span><span class="lnt">69
</span><span class="lnt">70
</span><span class="lnt">71
</span><span class="lnt">72
</span><span class="lnt">73
</span><span class="lnt">74
</span><span class="lnt">75
</span><span class="lnt">76
</span><span class="lnt">77
</span><span class="lnt">78
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">private class Itr implements Iterator&lt;E&gt; {
</span></span><span class="line"><span class="cl">    // 下一个元素的索引
</span></span><span class="line"><span class="cl">    int cursor;       // index of next element to return
</span></span><span class="line"><span class="cl">    // 最后一次返回元素的索引
</span></span><span class="line"><span class="cl">    int lastRet = -1; // index of last element returned; -1 if no such
</span></span><span class="line"><span class="cl">	// 这里的expectedModCount相当与一个锁
</span></span><span class="line"><span class="cl">    int expectedModCount = modCount;
</span></span><span class="line"><span class="cl">    Itr() {
</span></span><span class="line"><span class="cl">    }
</span></span><span class="line"><span class="cl">    // 是否有下一个元素
</span></span><span class="line"><span class="cl">    public boolean hasNext() {
</span></span><span class="line"><span class="cl">        return cursor != size;
</span></span><span class="line"><span class="cl">    }
</span></span><span class="line"><span class="cl">    // 返回list中的值
</span></span><span class="line"><span class="cl">    @SuppressWarnings(&#34;unchecked&#34;)
</span></span><span class="line"><span class="cl">    public E next() {
</span></span><span class="line"><span class="cl">        checkForComodification();
</span></span><span class="line"><span class="cl">        int i = cursor;
</span></span><span class="line"><span class="cl">        if (i &gt;= size)
</span></span><span class="line"><span class="cl">            throw new NoSuchElementException();
</span></span><span class="line"><span class="cl">        Object[] elementData = ArrayList.this.elementData;
</span></span><span class="line"><span class="cl">        // 第二次检查，list集合中数量是否发生变化
</span></span><span class="line"><span class="cl">        if (i &gt;= elementData.length)
</span></span><span class="line"><span class="cl">            throw new ConcurrentModificationException();
</span></span><span class="line"><span class="cl">        cursor = i + 1;  // 一下个元素的索引
</span></span><span class="line"><span class="cl">        return (E) elementData[lastRet = i];
</span></span><span class="line"><span class="cl">    }
</span></span><span class="line"><span class="cl">    public void remove() {
</span></span><span class="line"><span class="cl">        if (lastRet &lt; 0)
</span></span><span class="line"><span class="cl">            throw new IllegalStateException();
</span></span><span class="line"><span class="cl">        checkForComodification();
</span></span><span class="line"><span class="cl">        try {
</span></span><span class="line"><span class="cl">            // 移出list中元素
</span></span><span class="line"><span class="cl">            ArrayList.this.remove(lastRet);
</span></span><span class="line"><span class="cl">            // 由于cursor比lastRet大１，所以这行代码是将指针往回移动一位
</span></span><span class="line"><span class="cl">            cursor = lastRet;
</span></span><span class="line"><span class="cl">            lastRet = -1;
</span></span><span class="line"><span class="cl">            expectedModCount = modCount;
</span></span><span class="line"><span class="cl">        } catch (IndexOutOfBoundsException ex) {
</span></span><span class="line"><span class="cl">            throw new ConcurrentModificationException();
</span></span><span class="line"><span class="cl">        }
</span></span><span class="line"><span class="cl">    }
</span></span><span class="line"><span class="cl">    /**
</span></span><span class="line"><span class="cl">     * jdk1.8中使用的方法
</span></span><span class="line"><span class="cl">     * 将list中的所有元素都给了consumer，这个方法可以取出元素
</span></span><span class="line"><span class="cl">     *
</span></span><span class="line"><span class="cl">     * @param consumer
</span></span><span class="line"><span class="cl">     */
</span></span><span class="line"><span class="cl">    @Override
</span></span><span class="line"><span class="cl">    @SuppressWarnings(&#34;unchecked&#34;)
</span></span><span class="line"><span class="cl">    public void forEachRemaining(Consumer&lt;? super E&gt; consumer) {
</span></span><span class="line"><span class="cl">        Objects.requireNonNull(consumer);
</span></span><span class="line"><span class="cl">        final int size = ArrayList.this.size;
</span></span><span class="line"><span class="cl">        int i = cursor;
</span></span><span class="line"><span class="cl">        if (i &gt;= size) {
</span></span><span class="line"><span class="cl">            return;
</span></span><span class="line"><span class="cl">        }
</span></span><span class="line"><span class="cl">        final Object[] elementData = ArrayList.this.elementData;
</span></span><span class="line"><span class="cl">        if (i &gt;= elementData.length) {
</span></span><span class="line"><span class="cl">            throw new ConcurrentModificationException();
</span></span><span class="line"><span class="cl">        }
</span></span><span class="line"><span class="cl">        while (i != size &amp;&amp; modCount == expectedModCount) {
</span></span><span class="line"><span class="cl">            consumer.accept((E) elementData[i++]);
</span></span><span class="line"><span class="cl">        }
</span></span><span class="line"><span class="cl">        // update once at end of iteration to reduce heap write traffic
</span></span><span class="line"><span class="cl">        cursor = i;
</span></span><span class="line"><span class="cl">        lastRet = i - 1;
</span></span><span class="line"><span class="cl">        checkForComodification();
</span></span><span class="line"><span class="cl">    }
</span></span><span class="line"><span class="cl">    /**
</span></span><span class="line"><span class="cl">     * 检查这两个值是否相等
</span></span><span class="line"><span class="cl">     * 在迭代器时list集合中的元素数量发生变化时会造成这两个值不相等
</span></span><span class="line"><span class="cl">     */
</span></span><span class="line"><span class="cl">    final void checkForComodification() {
</span></span><span class="line"><span class="cl">        if (modCount != expectedModCount)
</span></span><span class="line"><span class="cl">            throw new ConcurrentModificationException();
</span></span><span class="line"><span class="cl">    }
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></td></tr></table>
</div>
</div><p>在这里有有很多的博客说他是工作的在一个独立的线程中的，而且也没有解释。如下：</p>
<blockquote>
<p>terator 是工作在一个独立的线程中，并且拥有一个 mutex 锁。 Iterator 被创建之后会建立一个指向原来对象的单链索引表，当原来的对象数量发生变化时，这个索引表的内容不会同步改变，所以当索引指针往后移动的时候就找不到要迭代的对象，所以按照 fail-fast 原则 Iterator 会马上抛出 java.util.ConcurrentModificationException 异常。
所以 Iterator 在工作的时候是不允许被迭代的对象被改变的。但你可以使用 Iterator 本身的方法 remove() 来删除对象， Iterator.remove() 方法会在删除当前迭代对象的同时维护索引的一致性</p>
</blockquote>
<p>Iterator中有一个锁，但是不是工作在一个独立的线程中的，它和ArrayList工作在同一个线程中，这个锁的是为了在使用迭代器的时候不能再向ArrayList中添加元素。
如下：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kn">import</span> <span class="nn">java.util.*</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">TestIterator</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">    <span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">List</span><span class="o">&lt;</span><span class="n">Integer</span><span class="o">&gt;</span> <span class="n">list</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ArrayList</span><span class="o">&lt;&gt;();</span>
</span></span><span class="line"><span class="cl">        <span class="n">list</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="mi">1</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">        <span class="n">list</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="mi">2</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">        <span class="n">Iterator</span> <span class="n">iterator</span> <span class="o">=</span> <span class="n">list</span><span class="o">.</span><span class="na">iterator</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">        <span class="n">list</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="mi">3</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">        <span class="k">if</span> <span class="o">(</span><span class="n">iterator</span><span class="o">.</span><span class="na">hasNext</span><span class="o">())</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">            <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">iterator</span><span class="o">.</span><span class="na">next</span><span class="o">());</span>
</span></span><span class="line"><span class="cl">        <span class="o">}</span>
</span></span><span class="line"><span class="cl">    <span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>运行上述的程序会报一个ConcurrentModificationException，这是因为当生成一个迭代器的时候会在会初始化一个变量expectedModCount，让这个变量expectedModCount = modCount，在上篇中说过了modCoun记录的是ArrayList的修改次数，ArrayList返回迭代器之后，然后再次使用ArrayList中的方法添加或者删除元素会导致modCount值变大，而在此调用迭代器中的方法的时候会检查expectedModCount和modCount是否相等，如果不相等则抛出ConcurrentModificationException。</p>
<p>下面的是官方文档中对ConcurrentModificationException异常的描述。</p>
<blockquote>
<p>public class ConcurrentModificationExceptionextends RuntimeException当方法检测到对象的并发修改，但不允许这种修改时，抛出此异常。</p>
</blockquote>
<blockquote>
<p>例如，某个线程在 Collection 上进行迭代时，通常不允许另一个线性修改该 Collection。通常在这些情况下，迭代的结果是不确定的。如果检测到这种行为，一些迭代器实现（包括 JRE 提供的所有通用 collection 实现）可能选择抛出此异常。执行该操作的迭代器称为快速失败 迭代器，因为迭代器很快就完全失败，而不会冒着在将来某个时间任意发生不确定行为的风险。</p>
</blockquote>
<blockquote>
<p>注意，此异常不会始终指出对象已经由不同 线程并发修改。如果单线程发出违反对象协定的方法调用序列，则该对象可能抛出此异常。例如，如果线程使用快速失败迭代器在 collection 上迭代时直接修改该 collection，则迭代器将抛出此异常。</p>
</blockquote>
<blockquote>
<p>注意，迭代器的快速失败行为无法得到保证，因为一般来说，不可能对是否出现不同步并发修改做出任何硬性保证。快速失败操作会尽最大努力抛出 ConcurrentModificationException。因此，为提高此类操作的正确性而编写一个依赖于此异常的程序是错误的做法，正确做法是：ConcurrentModificationException 应该仅用于检测 bug。</p>
</blockquote>
<p>这里面说道了fast-fail（快速失败）机制，fast-fail是Java集合（Collection）中的一种错误检测机制。当一个线程对集合使用迭代器遍历时，另一个线程修改了这个集合的内容。（这里的另一个进程也可能和遍历的是同一个进程）。
下面的两个程序中分别演示了在单线程和多线程中的情况。</p>
<ol>
<li>单线程</li>
</ol>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kn">import</span> <span class="nn">java.util.*</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">TestIterator</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">    <span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">List</span><span class="o">&lt;</span><span class="n">Integer</span><span class="o">&gt;</span> <span class="n">list</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ArrayList</span><span class="o">&lt;&gt;();</span>
</span></span><span class="line"><span class="cl">        <span class="n">list</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="mi">1</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">        <span class="n">list</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="mi">2</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">        <span class="n">Iterator</span> <span class="n">iterator</span> <span class="o">=</span> <span class="n">list</span><span class="o">.</span><span class="na">iterator</span><span class="o">();</span>
</span></span><span class="line"><span class="cl">        <span class="n">list</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="mi">3</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">        <span class="k">if</span> <span class="o">(</span><span class="n">iterator</span><span class="o">.</span><span class="na">hasNext</span><span class="o">())</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">            <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">iterator</span><span class="o">.</span><span class="na">next</span><span class="o">());</span>
</span></span><span class="line"><span class="cl">        <span class="o">}</span>
</span></span><span class="line"><span class="cl">    <span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>这里在迭代的过程中又添加了一个元素，之后在此迭代使用next时导致出现ConcurrentModificationException异常。
在多道程序中的情况：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">import java.util.*;
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">public class TestIteratorThread extends Thread{
</span></span><span class="line"><span class="cl">    static List&lt;Integer&gt; list;
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    public static void main(String[] args) {
</span></span><span class="line"><span class="cl">        TestIteratorThread test = new TestIteratorThread();
</span></span><span class="line"><span class="cl">        list = new ArrayList&lt;&gt;();
</span></span><span class="line"><span class="cl">        list.add(1);
</span></span><span class="line"><span class="cl">        list.add(2);
</span></span><span class="line"><span class="cl">        test.start();
</span></span><span class="line"><span class="cl">        for (int i = 3; i &lt; 100; i++) {
</span></span><span class="line"><span class="cl">            System.out.println(&#34;main&#34;);    
</span></span><span class="line"><span class="cl">            list.add(i);
</span></span><span class="line"><span class="cl">        }
</span></span><span class="line"><span class="cl">    }
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    public void run() {
</span></span><span class="line"><span class="cl">        Iterator itr = list.iterator();
</span></span><span class="line"><span class="cl">        while (itr.hasNext()) {
</span></span><span class="line"><span class="cl">            System.out.println(itr.next());
</span></span><span class="line"><span class="cl">        }
</span></span><span class="line"><span class="cl">    }
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></td></tr></table>
</div>
</div><p>ArrayList中还有另一个迭代器ListItr。ArrayList中的下面的这个方法会返回这个迭代器。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">/**
</span></span><span class="line"><span class="cl"> * 返回从指定索引到结束的list迭代器
</span></span><span class="line"><span class="cl"> * &lt;p&gt;The returned list iterator is &lt;a href=&#34;#fail-fast&#34;&gt;&lt;i&gt;fail-fast&lt;/i&gt;&lt;/a&gt;.
</span></span><span class="line"><span class="cl"> *
</span></span><span class="line"><span class="cl"> * @throws IndexOutOfBoundsException {@inheritDoc}
</span></span><span class="line"><span class="cl"> */
</span></span><span class="line"><span class="cl">public ListIterator&lt;E&gt; listIterator(int index) {
</span></span><span class="line"><span class="cl">	// 这里是一个index的检查
</span></span><span class="line"><span class="cl">    if (index &lt; 0 || index &gt; size)
</span></span><span class="line"><span class="cl">        throw new IndexOutOfBoundsException(&#34;Index: &#34; + index);
</span></span><span class="line"><span class="cl">    return new ListItr(index);
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></td></tr></table>
</div>
</div><div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">/**
</span></span><span class="line"><span class="cl"> * 返回一个从0开始的迭代器
</span></span><span class="line"><span class="cl"> * Returns a list iterator over the elements in this list (in proper
</span></span><span class="line"><span class="cl"> * sequence).
</span></span><span class="line"><span class="cl"> *
</span></span><span class="line"><span class="cl"> * &lt;p&gt;The returned list iterator is &lt;a href=&#34;#fail-fast&#34;&gt;&lt;i&gt;fail-fast&lt;/i&gt;&lt;/a&gt;.
</span></span><span class="line"><span class="cl"> *
</span></span><span class="line"><span class="cl"> * @see #listIterator(int)
</span></span><span class="line"><span class="cl"> */
</span></span><span class="line"><span class="cl">public ListIterator&lt;E&gt; listIterator() {
</span></span><span class="line"><span class="cl">    return new ListItr(0);
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></td></tr></table>
</div>
</div><p>ListItr也是ArrayList的一个内部类，是Itr的一个增强的版,它拥有Itr的所有功能，并且还拥有三个Itr不具有的功能。</p>
<ol>
<li>双向迭代</li>
<li>使用迭代器添加元素。</li>
<li>能够指定迭代器开始索引的位置。
ListItr继承了Itr，实现了ListIterator，可以说ListIterator是Iterator的List增强版。</li>
</ol>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="kd">public</span> <span class="kd">interface</span> <span class="nc">ListIterator</span><span class="o">&lt;</span><span class="n">E</span><span class="o">&gt;</span> <span class="kd">extends</span> <span class="n">Iterator</span><span class="o">&lt;</span><span class="n">E</span><span class="o">&gt;</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">    <span class="kt">boolean</span> <span class="nf">hasNext</span><span class="o">();</span>		<span class="c1">// 判断是否有下一个元素
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="n">E</span> <span class="nf">next</span><span class="o">();</span>				<span class="c1">// 返回下一个元素
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="kt">boolean</span> <span class="nf">hasPrevious</span><span class="o">();</span>	<span class="c1">// 判断是否有前一个元素
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="n">E</span> <span class="nf">previous</span><span class="o">();</span>			<span class="c1">// 返回前一个元素
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="kt">int</span> <span class="nf">nextIndex</span><span class="o">();</span>		<span class="c1">// 返回下一个元素的索引
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="kt">int</span> <span class="nf">previousIndex</span><span class="o">();</span>	<span class="c1">// 返回前一个元素的索引
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="kt">void</span> <span class="nf">remove</span><span class="o">();</span>			<span class="c1">// 删除当前元素
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="kt">void</span> <span class="nf">set</span><span class="o">(</span><span class="n">E</span> <span class="n">e</span><span class="o">);</span>			<span class="c1">// 重新设置元素
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="kt">void</span> <span class="nf">add</span><span class="o">(</span><span class="n">E</span> <span class="n">e</span><span class="o">);</span>			<span class="c1">// 添加元素
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>下面的是ArrayList中ListItr的源码</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span><span class="lnt">44
</span><span class="lnt">45
</span><span class="lnt">46
</span><span class="lnt">47
</span><span class="lnt">48
</span><span class="lnt">49
</span><span class="lnt">50
</span><span class="lnt">51
</span><span class="lnt">52
</span><span class="lnt">53
</span><span class="lnt">54
</span><span class="lnt">55
</span><span class="lnt">56
</span><span class="lnt">57
</span><span class="lnt">58
</span><span class="lnt">59
</span><span class="lnt">60
</span><span class="lnt">61
</span><span class="lnt">62
</span><span class="lnt">63
</span><span class="lnt">64
</span><span class="lnt">65
</span><span class="lnt">66
</span><span class="lnt">67
</span><span class="lnt">68
</span><span class="lnt">69
</span><span class="lnt">70
</span><span class="lnt">71
</span><span class="lnt">72
</span><span class="lnt">73
</span><span class="lnt">74
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">/**
</span></span><span class="line"><span class="cl"> * AbstractList中也有一个ListItr，ArrayList中的ListItr是对AbstractList中的一个优化。
</span></span><span class="line"><span class="cl"> * ListItr与普通Iterator的区别
</span></span><span class="line"><span class="cl"> * - 它可以进行双向移动，而普通的迭代器只能进行单向移动
</span></span><span class="line"><span class="cl"> * - 它可以添加元素，而普通的迭代器不行
</span></span><span class="line"><span class="cl"> * An optimized version of AbstractList.ListItr
</span></span><span class="line"><span class="cl"> */
</span></span><span class="line"><span class="cl">private class ListItr extends Itr implements ListIterator&lt;E&gt; {
</span></span><span class="line"><span class="cl">	// 这个构造器有一个index参数
</span></span><span class="line"><span class="cl">	// 这个参数的作用是是迭代器从index位置开始
</span></span><span class="line"><span class="cl">    ListItr(int index) {
</span></span><span class="line"><span class="cl">        super();
</span></span><span class="line"><span class="cl">        cursor = index;
</span></span><span class="line"><span class="cl">    }
</span></span><span class="line"><span class="cl">    // 是否有前一个元素
</span></span><span class="line"><span class="cl">    public boolean hasPrevious() {
</span></span><span class="line"><span class="cl">        return cursor != 0;
</span></span><span class="line"><span class="cl">    }
</span></span><span class="line"><span class="cl">    // 获取下一个元素的索引
</span></span><span class="line"><span class="cl">	// 这个方法没有做越界判断
</span></span><span class="line"><span class="cl">    public int nextIndex() {
</span></span><span class="line"><span class="cl">        return cursor;
</span></span><span class="line"><span class="cl">    }
</span></span><span class="line"><span class="cl">    /**
</span></span><span class="line"><span class="cl">     * 获取前一个元素的索引
</span></span><span class="line"><span class="cl">	 * 这个方法没有做越界判断
</span></span><span class="line"><span class="cl">     * @return
</span></span><span class="line"><span class="cl">     */
</span></span><span class="line"><span class="cl">    public int previousIndex() {
</span></span><span class="line"><span class="cl">        return cursor - 1;
</span></span><span class="line"><span class="cl">    }
</span></span><span class="line"><span class="cl">	
</span></span><span class="line"><span class="cl">    //　返回cursor前一元素
</span></span><span class="line"><span class="cl">    @SuppressWarnings(&#34;unchecked&#34;)
</span></span><span class="line"><span class="cl">    public E previous() {
</span></span><span class="line"><span class="cl">		// 像前面一样的检查
</span></span><span class="line"><span class="cl">        checkForComodification();
</span></span><span class="line"><span class="cl">        int i = cursor - 1;
</span></span><span class="line"><span class="cl">        if (i &lt; 0)  // 检查是否越界
</span></span><span class="line"><span class="cl">            throw new NoSuchElementException();
</span></span><span class="line"><span class="cl">		// 获取ArrayList中的元素
</span></span><span class="line"><span class="cl">        Object[] elementData = ArrayList.this.elementData;
</span></span><span class="line"><span class="cl">        if (i &gt;= elementData.length)	// 再次检查
</span></span><span class="line"><span class="cl">            throw new ConcurrentModificationException();
</span></span><span class="line"><span class="cl">        cursor = i;
</span></span><span class="line"><span class="cl">        return (E) elementData[lastRet = i];
</span></span><span class="line"><span class="cl">    }  
</span></span><span class="line"><span class="cl">	
</span></span><span class="line"><span class="cl">    public void set(E e) {
</span></span><span class="line"><span class="cl">		// 当添加或者删除元素的时候lastRet的值会变为-1, 初始化的时候也为-1
</span></span><span class="line"><span class="cl">        if (lastRet &lt; 0)
</span></span><span class="line"><span class="cl">            throw new IllegalStateException();
</span></span><span class="line"><span class="cl">        checkForComodification();
</span></span><span class="line"><span class="cl">        try {
</span></span><span class="line"><span class="cl">            // 将上下次返回元素的位置的元素设置为e
</span></span><span class="line"><span class="cl">            ArrayList.this.set(lastRet, e);
</span></span><span class="line"><span class="cl">        } catch (IndexOutOfBoundsException ex) {
</span></span><span class="line"><span class="cl">            throw new ConcurrentModificationException();
</span></span><span class="line"><span class="cl">        }
</span></span><span class="line"><span class="cl">    }
</span></span><span class="line"><span class="cl">    public void add(E e) {
</span></span><span class="line"><span class="cl">        checkForComodification();
</span></span><span class="line"><span class="cl">        try {
</span></span><span class="line"><span class="cl">            // 当前元素后移一位
</span></span><span class="line"><span class="cl">            int i = cursor;
</span></span><span class="line"><span class="cl">            ArrayList.this.add(i, e);   // 在i的位置添加元素e
</span></span><span class="line"><span class="cl">            cursor = i + 1;
</span></span><span class="line"><span class="cl">            lastRet = -1;
</span></span><span class="line"><span class="cl">            expectedModCount = modCount;
</span></span><span class="line"><span class="cl">        } catch (IndexOutOfBoundsException ex) {
</span></span><span class="line"><span class="cl">            throw new ConcurrentModificationException();
</span></span><span class="line"><span class="cl">        }
</span></span><span class="line"><span class="cl">    }
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></td></tr></table>
</div>
</div><p>这个也是一个快速失败的迭代器。</p>
<h4 id="arraylist中的子集合">ArrayList中的子集合</h4>
<p>在上篇中的toArray()方法中说过修改toArray返回的数组不会影响原集合，而下面说的子集合SubList会对原集合有影响，下面的这个方法会返回一个SubList.</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="cm">/**
</span></span></span><span class="line"><span class="cl"><span class="cm"> * 获取fromIndex到toIndex之间的子集合
</span></span></span><span class="line"><span class="cl"><span class="cm"> * - 若fromIndex == toIndex，则返回空集合
</span></span></span><span class="line"><span class="cl"><span class="cm"> * - 对该子集合操作，会影响原有的集合
</span></span></span><span class="line"><span class="cl"><span class="cm"> * - 当调用了subList()后，若对原有的集合进行操作，会抛出java.util.ConcurrentModificationExcept
</span></span></span><span class="line"><span class="cl"><span class="cm"> * 这个和Itr的原因差不多由于modCount发生了改变，对集合的操作需要用子集合提供的方法
</span></span></span><span class="line"><span class="cl"><span class="cm"> * - 该子集合支持所有的集合操作
</span></span></span><span class="line"><span class="cl"><span class="cm"> * @throws IndexOutOfBoundsException {@inheritDoc}
</span></span></span><span class="line"><span class="cl"><span class="cm"> * @throws IllegalArgumentException  {@inheritDoc}
</span></span></span><span class="line"><span class="cl"><span class="cm"> */</span>
</span></span><span class="line"><span class="cl"><span class="kd">public</span> <span class="n">List</span><span class="o">&lt;</span><span class="n">E</span><span class="o">&gt;</span> <span class="nf">subList</span><span class="o">(</span><span class="kt">int</span> <span class="n">fromIndex</span><span class="o">,</span> <span class="kt">int</span> <span class="n">toIndex</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">subListRangeCheck</span><span class="o">(</span><span class="n">fromIndex</span><span class="o">,</span> <span class="n">toIndex</span><span class="o">,</span> <span class="n">size</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">    <span class="k">return</span> <span class="k">new</span> <span class="n">ArrayList</span><span class="o">.</span><span class="na">SubList</span><span class="o">(</span><span class="k">this</span><span class="o">,</span> <span class="mi">0</span><span class="o">,</span> <span class="n">fromIndex</span><span class="o">,</span> <span class="n">toIndex</span><span class="o">);</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>这里会一个方法检查传入值的合法性：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-java" data-lang="java"><span class="line"><span class="cl"><span class="c1">// 检查传入值的合法性
</span></span></span><span class="line"><span class="cl"><span class="c1">// 注意[fromIndex, toIndex)
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="kd">static</span> <span class="kt">void</span> <span class="nf">subListRangeCheck</span><span class="o">(</span><span class="kt">int</span> <span class="n">fromIndex</span><span class="o">,</span> <span class="kt">int</span> <span class="n">toIndex</span><span class="o">,</span> <span class="kt">int</span> <span class="n">size</span><span class="o">)</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">    <span class="k">if</span> <span class="o">(</span><span class="n">fromIndex</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="o">)</span>
</span></span><span class="line"><span class="cl">        <span class="k">throw</span> <span class="k">new</span> <span class="n">IndexOutOfBoundsException</span><span class="o">(</span><span class="s">&#34;fromIndex = &#34;</span> <span class="o">+</span> <span class="n">fromIndex</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">    <span class="k">if</span> <span class="o">(</span><span class="n">toIndex</span> <span class="o">&gt;</span> <span class="n">size</span><span class="o">)</span>
</span></span><span class="line"><span class="cl">        <span class="k">throw</span> <span class="k">new</span> <span class="n">IndexOutOfBoundsException</span><span class="o">(</span><span class="s">&#34;toIndex = &#34;</span> <span class="o">+</span> <span class="n">toIndex</span><span class="o">);</span>
</span></span><span class="line"><span class="cl">    <span class="k">if</span> <span class="o">(</span><span class="n">fromIndex</span> <span class="o">&gt;</span> <span class="n">toIndex</span><span class="o">)</span>
</span></span><span class="line"><span class="cl">        <span class="k">throw</span> <span class="k">new</span> <span class="n">IllegalArgumentException</span><span class="o">(</span><span class="s">&#34;fromIndex(&#34;</span> <span class="o">+</span> <span class="n">fromIndex</span> <span class="o">+</span>
</span></span><span class="line"><span class="cl">                <span class="s">&#34;) &gt; toIndex(&#34;</span> <span class="o">+</span> <span class="n">toIndex</span> <span class="o">+</span> <span class="s">&#34;)&#34;</span><span class="o">);</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>SubList是ArrayList的私有的内部类
首先，看一下SubList中的几个变量：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">private final AbstractList&lt;E&gt; parent;   // 实际传入的是ArrayList本身
</span></span><span class="line"><span class="cl">private final int parentOffset;         // 相对于父集合的偏移量，其实就是fromIndex
</span></span><span class="line"><span class="cl">private final int offset;               // 偏移量，默认为0
</span></span><span class="line"><span class="cl">int size;                               // SubList中的元素个数
</span></span></code></pre></td></tr></table>
</div>
</div><p>下面的是SubList的构造器</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">/*
</span></span><span class="line"><span class="cl"> * 子集合中的元素只是将父集合的元素映射，并没有复制
</span></span><span class="line"><span class="cl"> * 因为有final的修饰，所以截取子集合后，父集合不能删除SubList中的有个元素——offset不能更改
</span></span><span class="line"><span class="cl"> *
</span></span><span class="line"><span class="cl"> * @param parent
</span></span><span class="line"><span class="cl"> * @param offset
</span></span><span class="line"><span class="cl"> * @param fromIndex
</span></span><span class="line"><span class="cl"> * @param toIndex
</span></span><span class="line"><span class="cl"> */
</span></span><span class="line"><span class="cl">SubList(AbstractList&lt;E&gt; parent,
</span></span><span class="line"><span class="cl">        int offset, int fromIndex, int toIndex) {
</span></span><span class="line"><span class="cl">    this.parent = parent;
</span></span><span class="line"><span class="cl">    this.parentOffset = fromIndex;
</span></span><span class="line"><span class="cl">    this.offset = offset + fromIndex;
</span></span><span class="line"><span class="cl">    this.size = toIndex - fromIndex;
</span></span><span class="line"><span class="cl">    this.modCount = ArrayList.this.modCount;
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></td></tr></table>
</div>
</div><p>SubList并没有复制一份ArrayList，而是建立了和ArrayList中elemntData数组之间的一一映射。下面的这个方法体现了这一点：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">/**
</span></span><span class="line"><span class="cl"> * 设置新值，返回旧值
</span></span><span class="line"><span class="cl"> *
</span></span><span class="line"><span class="cl"> * @param index
</span></span><span class="line"><span class="cl"> * @param e
</span></span><span class="line"><span class="cl"> * @return
</span></span><span class="line"><span class="cl"> */
</span></span><span class="line"><span class="cl">public E set(int index, E e) {
</span></span><span class="line"><span class="cl">    rangeCheck(index);          // 检查越界
</span></span><span class="line"><span class="cl">    checkForComodification();   //
</span></span><span class="line"><span class="cl">    // 从这里可以看出：对子集合添加元素，是直接对父类添加的
</span></span><span class="line"><span class="cl">    E oldValue = ArrayList.this.elementData(offset + index);
</span></span><span class="line"><span class="cl">    ArrayList.this.elementData[offset + index] = e;
</span></span><span class="line"><span class="cl">    return oldValue;
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></td></tr></table>
</div>
</div><p>可以看到这里直接使用了index+offset的方法访问，和前面的迭代器一样这里也有一个checkForComodification()方法，它的作用和迭代器中的那个方法作用一样。也就是说如果在程序中使用了ArrayList中的SubList，那么在使用SubList的期间就不能在使用原来的ArrayList进行修改。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">private void checkForComodification() {
</span></span><span class="line"><span class="cl">    if (ArrayList.this.modCount != this.modCount)
</span></span><span class="line"><span class="cl">        throw new ConcurrentModificationException();
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></td></tr></table>
</div>
</div><p>下面在看一个add()方法：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span><span class="lnt">9
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback"><span class="line"><span class="cl">// 添加元素
</span></span><span class="line"><span class="cl">public void add(int index, E e) {
</span></span><span class="line"><span class="cl">    rangeCheckForAdd(index);
</span></span><span class="line"><span class="cl">    checkForComodification();
</span></span><span class="line"><span class="cl">    // 中这里也可以看，index+offset得到添加的索引在父类中的索引，然后调用父类的方法。
</span></span><span class="line"><span class="cl">    parent.add(parentOffset + index, e);
</span></span><span class="line"><span class="cl">    this.modCount = parent.modCount;
</span></span><span class="line"><span class="cl">    this.size++;
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></td></tr></table>
</div>
</div><p>这个方法之间使用了ArrayList中的add()方法。其他的方法都是类似与上面的两个方法。在这里就不多做描述了。</p>
</div><div class="post-footer" id="post-footer">
    <div class="post-info">
        <div class="post-info-line">
            <div class="post-info-mod">
                <span>更新于 2018-05-29</span>
            </div></div>
        <div class="post-info-line">
            <div class="post-info-md"><span>
                            <a class="link-to-markdown" href="/arraylist-%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E4%B8%AD/index.md" target="_blank">阅读原始文档</a>
                        </span></div>
            <div class="post-info-share">
                <span><a href="javascript:void(0);" title="分享到 Twitter" data-sharer="twitter" data-url="http://example.org/arraylist-%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E4%B8%AD/" data-title="ArrayList源码阅读（中）——迭代器和子集合" data-hashtags="Java"><i class="fab fa-twitter fa-fw" aria-hidden="true"></i></a><a href="javascript:void(0);" title="分享到 Facebook" data-sharer="facebook" data-url="http://example.org/arraylist-%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E4%B8%AD/" data-hashtag="Java"><i class="fab fa-facebook-square fa-fw" aria-hidden="true"></i></a><a href="javascript:void(0);" title="分享到 Hacker News" data-sharer="hackernews" data-url="http://example.org/arraylist-%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E4%B8%AD/" data-title="ArrayList源码阅读（中）——迭代器和子集合"><i class="fab fa-hacker-news fa-fw" aria-hidden="true"></i></a><a href="javascript:void(0);" title="分享到 Line" data-sharer="line" data-url="http://example.org/arraylist-%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E4%B8%AD/" data-title="ArrayList源码阅读（中）——迭代器和子集合"><i data-svg-src="https://cdn.jsdelivr.net/npm/simple-icons@7.3.0/icons/line.svg" aria-hidden="true"></i></a><a href="javascript:void(0);" title="分享到 微博" data-sharer="weibo" data-url="http://example.org/arraylist-%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E4%B8%AD/" data-title="ArrayList源码阅读（中）——迭代器和子集合" data-ralateuid="xxxx"><i class="fab fa-weibo fa-fw" aria-hidden="true"></i></a></span>
            </div>
        </div>
    </div>

    <div class="post-info-more">
        <section class="post-tags"><i class="fas fa-tags fa-fw" aria-hidden="true"></i>&nbsp;<a href="/tags/java/">Java</a></section>
        <section>
            <span><a href="javascript:void(0);" onclick="window.history.back();">返回</a></span>&nbsp;|&nbsp;<span><a href="/">主页</a></span>
        </section>
    </div>

    <div class="post-nav"><a href="/arraylist%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E4%B8%8A/" class="prev" rel="prev" title="ArrayList源码阅读（上）"><i class="fas fa-angle-left fa-fw" aria-hidden="true"></i>ArrayList源码阅读（上）</a>
            <a href="/arraylist%E6%BA%90%E7%A0%81%E9%98%85%E8%AF%BB%E4%B8%8B/" class="next" rel="next" title="ArrayList源码分析（下）——Java8中新增的Spliterator的分析">ArrayList源码分析（下）——Java8中新增的Spliterator的分析<i class="fas fa-angle-right fa-fw" aria-hidden="true"></i></a></div>
</div>
<div id="comments"><div id="valine" class="comment"></div><noscript>
                Please enable JavaScript to view the comments powered by <a href="https://valine.js.org/">Valine</a>.
            </noscript></div></article></div>
            </main><footer class="footer">
        <div class="footer-container"><div class="footer-line">由 <a href="https://gohugo.io/" target="_blank" rel="noopener noreffer" title="Hugo 0.109.0">Hugo</a> 强力驱动 | 主题 - <a href="https://github.com/dillonzq/LoveIt" target="_blank" rel="noopener noreffer" title="LoveIt 0.2.11"><i class="far fa-kiss-wink-heart fa-fw" aria-hidden="true"></i> LoveIt</a>
                </div><div class="footer-line" itemscope itemtype="http://schema.org/CreativeWork"><i class="far fa-copyright fa-fw" aria-hidden="true"></i><span itemprop="copyrightYear">2023</span><span class="author" itemprop="copyrightHolder">&nbsp;<a href="/" target="_blank">MC</a></span>&nbsp;|&nbsp;<span class="license"><a rel="license external nofollow noopener noreffer" href="https://creativecommons.org/licenses/by-nc/4.0/" target="_blank">CC BY-NC 4.0</a></span></div>
        </div>
    </footer></div>

        <div id="fixed-buttons"><a href="#" id="back-to-top" class="fixed-button" title="回到顶部">
                <i class="fas fa-arrow-up fa-fw" aria-hidden="true"></i>
            </a><a href="#" id="view-comments" class="fixed-button" title="查看评论">
                <i class="fas fa-comment fa-fw" aria-hidden="true"></i>
            </a>
        </div><link rel="stylesheet" href="/lib/valine/valine.min.css"><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/valine@1.5.0/dist/Valine.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/autocomplete.js@0.38.1/dist/autocomplete.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lunr@2.3.9/lunr.min.js"></script><script type="text/javascript" src="/lib/lunr/lunr.stemmer.support.min.js"></script><script type="text/javascript" src="/lib/lunr/lunr.zh.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lazysizes@5.3.2/lazysizes.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/clipboard@2.0.11/dist/clipboard.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/sharer.js@0.5.1/sharer.min.js"></script><script type="text/javascript">window.config={"code":{"copyTitle":"复制到剪贴板","maxShownLines":50},"comment":{"valine":{"appId":"QGzwQXOqs5JOhN4RGPOkR2mR-MdYXbMMI","appKey":"WBmoGyJtbqUswvfLh6L8iEBr","avatar":"mp","el":"#valine","emojiCDN":"https://cdn.jsdelivr.net/npm/emoji-datasource-google@14.0.0/img/google/64/","emojiMaps":{"100":"1f4af.png","alien":"1f47d.png","anger":"1f4a2.png","angry":"1f620.png","anguished":"1f627.png","astonished":"1f632.png","black_heart":"1f5a4.png","blue_heart":"1f499.png","blush":"1f60a.png","bomb":"1f4a3.png","boom":"1f4a5.png","broken_heart":"1f494.png","brown_heart":"1f90e.png","clown_face":"1f921.png","cold_face":"1f976.png","cold_sweat":"1f630.png","confounded":"1f616.png","confused":"1f615.png","cry":"1f622.png","crying_cat_face":"1f63f.png","cupid":"1f498.png","dash":"1f4a8.png","disappointed":"1f61e.png","disappointed_relieved":"1f625.png","dizzy":"1f4ab.png","dizzy_face":"1f635.png","drooling_face":"1f924.png","exploding_head":"1f92f.png","expressionless":"1f611.png","face_vomiting":"1f92e.png","face_with_cowboy_hat":"1f920.png","face_with_hand_over_mouth":"1f92d.png","face_with_head_bandage":"1f915.png","face_with_monocle":"1f9d0.png","face_with_raised_eyebrow":"1f928.png","face_with_rolling_eyes":"1f644.png","face_with_symbols_on_mouth":"1f92c.png","face_with_thermometer":"1f912.png","fearful":"1f628.png","flushed":"1f633.png","frowning":"1f626.png","ghost":"1f47b.png","gift_heart":"1f49d.png","green_heart":"1f49a.png","grimacing":"1f62c.png","grin":"1f601.png","grinning":"1f600.png","hankey":"1f4a9.png","hear_no_evil":"1f649.png","heart":"2764-fe0f.png","heart_decoration":"1f49f.png","heart_eyes":"1f60d.png","heart_eyes_cat":"1f63b.png","heartbeat":"1f493.png","heartpulse":"1f497.png","heavy_heart_exclamation_mark_ornament":"2763-fe0f.png","hole":"1f573-fe0f.png","hot_face":"1f975.png","hugging_face":"1f917.png","hushed":"1f62f.png","imp":"1f47f.png","innocent":"1f607.png","japanese_goblin":"1f47a.png","japanese_ogre":"1f479.png","joy":"1f602.png","joy_cat":"1f639.png","kiss":"1f48b.png","kissing":"1f617.png","kissing_cat":"1f63d.png","kissing_closed_eyes":"1f61a.png","kissing_heart":"1f618.png","kissing_smiling_eyes":"1f619.png","laughing":"1f606.png","left_speech_bubble":"1f5e8-fe0f.png","love_letter":"1f48c.png","lying_face":"1f925.png","mask":"1f637.png","money_mouth_face":"1f911.png","nauseated_face":"1f922.png","nerd_face":"1f913.png","neutral_face":"1f610.png","no_mouth":"1f636.png","open_mouth":"1f62e.png","orange_heart":"1f9e1.png","partying_face":"1f973.png","pensive":"1f614.png","persevere":"1f623.png","pleading_face":"1f97a.png","pouting_cat":"1f63e.png","purple_heart":"1f49c.png","rage":"1f621.png","relaxed":"263a-fe0f.png","relieved":"1f60c.png","revolving_hearts":"1f49e.png","right_anger_bubble":"1f5ef-fe0f.png","robot_face":"1f916.png","rolling_on_the_floor_laughing":"1f923.png","scream":"1f631.png","scream_cat":"1f640.png","see_no_evil":"1f648.png","shushing_face":"1f92b.png","skull":"1f480.png","skull_and_crossbones":"2620-fe0f.png","sleeping":"1f634.png","sleepy":"1f62a.png","slightly_frowning_face":"1f641.png","slightly_smiling_face":"1f642.png","smile":"1f604.png","smile_cat":"1f638.png","smiley":"1f603.png","smiley_cat":"1f63a.png","smiling_face_with_3_hearts":"1f970.png","smiling_imp":"1f608.png","smirk":"1f60f.png","smirk_cat":"1f63c.png","sneezing_face":"1f927.png","sob":"1f62d.png","space_invader":"1f47e.png","sparkling_heart":"1f496.png","speak_no_evil":"1f64a.png","speech_balloon":"1f4ac.png","star-struck":"1f929.png","stuck_out_tongue":"1f61b.png","stuck_out_tongue_closed_eyes":"1f61d.png","stuck_out_tongue_winking_eye":"1f61c.png","sunglasses":"1f60e.png","sweat":"1f613.png","sweat_drops":"1f4a6.png","sweat_smile":"1f605.png","thinking_face":"1f914.png","thought_balloon":"1f4ad.png","tired_face":"1f62b.png","triumph":"1f624.png","two_hearts":"1f495.png","unamused":"1f612.png","upside_down_face":"1f643.png","weary":"1f629.png","white_frowning_face":"2639-fe0f.png","white_heart":"1f90d.png","wink":"1f609.png","woozy_face":"1f974.png","worried":"1f61f.png","yawning_face":"1f971.png","yellow_heart":"1f49b.png","yum":"1f60b.png","zany_face":"1f92a.png","zipper_mouth_face":"1f910.png","zzz":"1f4a4.png"},"enableQQ":false,"highlight":true,"lang":"zh-CN","pageSize":10,"placeholder":"你的评论 ...","recordIP":true,"serverURLs":"https://leancloud.hugoloveit.com","visitor":true}},"search":{"highlightTag":"em","lunrIndexURL":"/index.json","lunrLanguageCode":"zh","lunrSegmentitURL":"/lib/lunr/lunr.segmentit.js","maxResultLength":10,"noResultsFound":"没有找到结果","snippetLength":50,"type":"lunr"}};</script><script type="text/javascript" src="/js/theme.min.js"></script></body>
</html>
